<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        div {
            width: 300px;
            height: 300px;
            background-color: aquamarine;
        }
    </style>
</head>

<body>
    <div></div>
    <a href="https://www.baidu.com" id="link">百度</a>

    <script>
        var div = document.querySelector('div');
        div.onclick = function (e) {
            //兼容性处理
            e = e || window.event;
            alert('div');
            //阻止冒泡
            e.stopPropagation();
        }
        document.body.onclick = function () {
            alert('body');
        }

        //有些网页元素是有默认行为的,比如超链接跳转，提交按钮提交表单数据
        //
        document.querySelector('#link').onclick = function (e) {
            e = e || window.event;
            //如果选择不跳转，就不跳转
            if (!confirm('跳转？')) {
                alert('no');
                //阻止默认行为
                e.preventDefault();
            }
        }
    </script>
</body>

</html>